package net.pa.ovpn.utils;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import net.pa.ovpn.constant.ovpnConstant;
import net.pa.ovpn.model.FileObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {
		
	
	public static int createFileRecord(String filePath, List<FileObject> fileObjects) {
		
		String fileName = filePath + File.separator + ovpnConstant.fileRecord;

		/** 返回操作结果, 0表失败, 1表成功 */
		int returnValue = 0;

		/** 建立document对象 */
		Document document = DocumentHelper.createDocument();
		/** 建立XML文档的根books */
		Element root = document.addElement("files");

		for (FileObject fo : fileObjects) {
			String id = fo.getId();
			String name = fo.getName();
			String path = fo.getPath();

			Element fileElement = root.addElement("file");
			fileElement.addAttribute("id", id);

			Element nameElement = fileElement.addElement("name");
			nameElement.setText(name);

			Element pathElement = fileElement.addElement("path");
			pathElement.setText(path);

		}

		try {
			/** 将document中的内容写入文件中 */
			XMLWriter writer = new XMLWriter(new FileWriter(new File(fileName)),OutputFormat.createPrettyPrint());
			writer.write(document);
			writer.close();
			/** 执行成功,需返回1 */
			returnValue = 1;
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return returnValue;
	}
	
	public static List<FileObject> readFileRecord(String filePath){
		
		List<FileObject> result = new ArrayList<FileObject>();;
		String fileName = filePath + File.separator + ovpnConstant.fileRecord;
		
		File xmlFile = new File(fileName);
		if(xmlFile.exists()){
			try {
				SAXReader reader = new SAXReader();
				Document doc = reader.read(fileName);
				
				@SuppressWarnings("unchecked")
				List<Element> fileNodes = doc.selectNodes("/files/file");
				for (Element element : fileNodes) {
					String id = element.attributeValue("id");
					String name = element.element("name").getText();
					String path = element.elementText("path");
					
					FileObject fo = new FileObject();
					fo.setId(id);
					fo.setName(name);
					fo.setPath(path);
					
					result.add(fo);
				}
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 			
		}else{
			logger.info(fileName + " not found.");
		}
		
		return result;
	}

	public static List<File> getFilelist(String filePath) throws Exception {
		filelist.clear();
		if (filePath == null || filePath.length() == 0) {
			throw new Exception("File path can not be null.");
		} else {
			getFiles(filePath);
		}
		return filelist;
	}

	private static void getFiles(String filePath) {
		File root = new File(filePath);
		File[] files = root.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				getFiles(file.getAbsolutePath());
			} else {
				if(!file.getName().equals(ovpnConstant.fileRecord)){
					filelist.add(file);					
				}
			}
		}
	}

	private static List<File> filelist = new ArrayList<File>();	
	
	private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
	
	public static void main(String[] args) {
		readFileRecord("D:\\Downloads");
	}

}
